/* eslint-disable */
'use strict'
const express = require('express')
const router = express.Router()
const log4js = require('log4js')
const logger = log4js.getLogger('users')
const dbconnection = require('../config/database.js')
const encrypt = require('../utils/encryptTool.js')
const cookieTool = require('../utils/cookieTool.js')
const dateFormat = require('../utils/dateFormat')

const insertUser = (user, callback) => {
  dbconnection.execute('INSERT INTO users SET ?', user, callback)
}

const selectUser = (uuid, callback) => {
  dbconnection.execute('SELECT uuid FROM users WHERE uuid = ?', uuid, callback)
}

const checkUser = (mobile, password, callback) => {
  dbconnection.execute('SELECT * FROM users WHERE mobile = ? and password = ?', [mobile, password], callback)
}

const selectUserPromise = uuid => new Promise((resolve, reject) => {
  selectUser(uuid, (err, results, fields) => {
    if (err) {
      logger.error(err)
      reject(err)
    } else {
      resolve(results)
    }
  })
})

const checkUserPromise = (mobile, password) => new Promise((resolve, reject) => {
  checkUser(mobile, password, (err, results, fields) => {
    if (err) {
      logger.error(err)
      reject(err)
    } else {
      resolve(results)
    }
  })
})

/* POST register listing. */
router.post('/register', function(req, res, next) {
  let date = new Date()
  let jointime = dateFormat(date, 'yyyy-MM-dd')
  let user = {}
  if (req.body.flag && req.body.flag !== '') {
    user = {
      uuid: encrypt(req.body.mobile),
      password: req.body.password,
      mobile: req.body.mobile,
      jointime: jointime,
      email: req.body.email,
      flag: encrypt(req.body.flag)
    }
  } else {
    user = {
      uuid: encrypt(req.body.mobile),
      password: req.body.password,
      mobile: req.body.mobile,
      address: req.body.address,
      email: req.body.email,
      code: req.body.code,
      birthday: req.body.birthday,
      jointime: jointime,
      flag: encrypt(req.body.mobile)
    }
  }
  selectUserPromise(encrypt(req.body.mobile)).then((results) => {
    if (results.length > 0) {
      res.send({rtnCode: '300', rtnMsg: '该手机号已经注册，请勿重新操作！'})
    } else {
      insertUser(user, (err, results, fields) => {
        if (err) {
          logger.error(err)
          res.status(500).send({rtnCode: '500', rtnMsg: '服务器错误'})
          throw err
        }
        cookieTool(req, res, encrypt(req.body.mobile), user)
        res.send({rtnCode: '000', rtnMsg: '恭喜您，注册成功！', redirect: '/'})
      })
    }
  }).catch((err) => {
    logger.error(err)
    res.status(500).send({rtnCode: '500', rtnMsg: '服务器错误'})
  })
});

router.post('/login', function(req, res, next) {
  checkUserPromise(req.body.mobile, req.body.password).then(results => {
    if (results.length > 0) {
      logger.info(results)
      cookieTool(req, res, encrypt(req.body.mobile), results[0])
      res.send({rtnCode: '000', rtnMsg: '恭喜您，登录成功！', redirect: '/'})
    } else {
      res.send({rtnCode: '300', rtnMsg: '该手机号尚未注册或者密码错误！'})
    }
  }).catch(err => {
    logger.error(err)
    res.status(500).send({rtnCode: '500', rtnMsg: '服务器错误'})
  })
});

router.post('/logout', function(req, res, next) {
  
  res.send('respond with a logout');
});


module.exports = router;
